< previous page page_189 next page >

Page 189
Examples Filename: PassParms.frm and Func.bas
In the preceding code, the vntName variable is checked within an If statement to determine whether it was passed in. If it was not passed in, a default value can be assigned to the missing parameter. Heres an example of how you might call the ProcOptionalVar() procedure:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdOptionalVar_Click()
   ' Call with parameter filled in
   Call ProcOptionalVar(1, 3, "Bill Gates")
   ' Call with no 2nd parameter
   Call ProcOptionalVar(2, 5)
End Sub
Optional Argument with a Data Type
The second way to declare an optional argument is to give it a data type, just like you would normally. In fact, this is the preferred method, because variants carry a lot of overhead. Heres another procedure that has an optional argument with a data type declared:
9f31265bb40b592b1929e18a47ab28e5.gif
Public Sub ProcOptional(ByVal intFirst As Integer, _
                      ByVal intSecond As Integer, _
                      Optional intValue As Integer = -1)

   If intValue = -1 Then
      MsgBox "The value was not passed in"
   Else
      MsgBox "The value is " & intValue
   End If

End Sub
This example assigns the argument a default value right in the declaration. This way, if the argument does not receive a value, it will be assigned the default value specified. When you declare an argument with a data type, you cannot use the IsMissing() function. Instead, you just need to check this value against the default value to see whether they are the same. Obviously, you want to choose a default value thats something that would never be passed into this procedure. Otherwise, you wouldnt know whether it was passed or assigned to the default value.

 
< previous page page_189 next page >

If you like this book, buy it!